import Api from '@/api'
import ls from '@/lib/ls'
import dayjs from 'dayjs'
// 配置

const maps = {
    // 商品信息
    goodsList:{
        get: () =>Api.Common.PcObjProcess.Do({ "functionName": "getGoodsInfoTerminal", "contentData": {"terminalType":"3","currentTime":''} }),
        set: function(resp) {
            let obj = JSON.parse(JSON.stringify(resp))
            console.log(obj);
            
            try {
                // if(this.shopInfo && resp.result){
                //     resp.result.forEach(item => {
                //         if (!item.attachment) {
                //           item.attachment = this.shopInfo.WaiterMaterialPic;
                //         }
                //       });
                // }
                // this.goodsList = JSON.parse(JSON.stringify(resp.result).replaceAll('\\"', '"').replaceAll('"[', "[").replaceAll(']"', "]"))
                this.goodsList = JSON.parse(JSON.stringify(resp.result))
                console.log(this.goodsList);
                
            } catch (error) {
                console.error(error)
            }
        }
    },
}


export default {
    async UpdateAll() {
        const awaits = Object.values(maps)
        const promises = awaits.map(c => c.get())
        const results = await Promise.allSettled(promises)
        results.forEach((result, index) => {
            if (result.status === 'fulfilled') {
                const resp = result.value
                awaits[index].set.bind(this)(resp)
            } else {
                console.log('接口报错:', result.reason)
                // 可以选择继续执行其他接口请求，或者忽略错误并继续执行下面的代码
            }
        })
    },
    // 更新配置
    async Update(...args) {
        const awaits = args.map((configName) => maps[configName])
        const resps = await Promise.all(awaits.map(c => c.get()))
        awaits.forEach((item, index) => {
            item.set.bind(this)(resps[index])
        })
    }
}